package com.bw.ppjokeplay.mvvm.viewmodel.home

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.ppjokeplay.adapter.CommentsAdapter
import com.bw.ppjokeplay.commen.entity.BaseEntity
import com.bw.ppjokeplay.commen.mvvm.viewmodel.BaseViewModel
import com.bw.ppjokeplay.mvvm.model.entity.AddCommentsEntity
import com.bw.ppjokeplay.mvvm.model.entity.CommentsEntity
import com.bw.ppjokeplay.mvvm.model.home.CommentsModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author Lishaohe
 * @description:
 * @date :2021/11/11 0011 下午 1:11
 *
 */
class CommentsViewModel : BaseViewModel<CommentsModel>() {



   private var id : Int=0
   private var itemId:Long=0
   private val pageCount:Int=10
   private var userId:Int=0
    public  var head1:String?=null
    public  var name1:String?=null
    public  var text1:String?=null
    public  var description1:String?=null
    public  var url1:String?=null



//   var commentText:String ?=null
   var height:Int =0
   var image_url:String? ="0"
   var userId1:Int =0
   var video_url:String ?="0"
   var width:Int =0
    var commentsAdapter : CommentsAdapter = CommentsAdapter()
    var commenttext :ObservableField<String> = ObservableField()
    override fun initdata() {
        m.requestComments(id, itemId, pageCount, userId)!!
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this)

    }

    override fun result(t: BaseEntity) {
        if (t is CommentsEntity){
            val commentsEntity = t as CommentsEntity
            LogUtils.e(commentsEntity.data!!.data)
            commentsAdapter.setNewInstance(commentsEntity.data!!.data)
        }else if (t is AddCommentsEntity){
            val addCommentsEntity = t as AddCommentsEntity
            if (addCommentsEntity.message.equals("成功")){
                ToastUtils.showLong("发表成功！")
                initdata()
                commentsAdapter.notifyDataSetChanged()
            }

        }
    }

    fun vis(id: Any?, head: Any?, name: Any?, text: Any?, description: Any?, url: Any?) {
        itemId= id as Long
        head1 = head as String?
        name1 = name as String?
        text1 = text as String?
        description1 = description as String?
        url1 = url as String?
    }
    fun finish(){
        finishPage()
    }

    fun sendcomment(){

        m.requestaddComments(commenttext.get()!!,height,image_url!!,itemId,userId1,video_url!!,width)!!
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this)


    }

}